<template><div><h2 id="请求限流" tabindex="-1"><a class="header-anchor" href="#请求限流"><span>请求限流</span></a></h2>
<p>Laravel 10 中文文档 /</p>
<h2 id="限流" tabindex="-1"><a class="header-anchor" href="#限流"><span>限流</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#cache-configuration">缓存配置</a></li>
</ul>
</li>
<li><a href="#basic-usage">基础用法</a>
<ul>
<li><a href="#manually-incrementing-attempts">手动增加请求次数</a></li>
<li><a href="#clearing-attempts">清除请求</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 包含了一个开箱即用的，基于 <a href="https://learnku.com/docs/laravel/10.x/rate-limiting/cache" target="_blank" rel="noopener noreferrer">缓存</a> 实现的限流器，提供了一个简单的方法来限制指定时间内的任何操作。</p>
<blockquote>
<p><strong>技巧</strong><br>
了解更多关于如何限制 HTTP 请求，请参考 <a href="https://learnku.com/docs/laravel/10.x/rate-limiting/routing#rate-limiting" target="_blank" rel="noopener noreferrer">请求频率限制中间件</a>。</p>
</blockquote>
<h3 id="缓存配置" tabindex="-1"><a class="header-anchor" href="#缓存配置"><span>缓存配置</span></a></h3>
<p>通常情况下，限流器使用你默认的缓存驱动，由 <code v-pre>cache</code> 配置文件中的 <code v-pre>default</code> 键定义。你也可以通过在你的应用程序的 <code v-pre>cache</code> 配置文件中定义一个 <code v-pre>limiter</code> 来指定限流器应该使用哪一个缓存来驱动：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'memcached'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'limiter'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<p>可以通过 <code v-pre>Illuminate\Support\Facades\RateLimiter</code> 来操作限流器。限流器提供的最简单的方法是 <code v-pre>attempt</code> 方法，它将一个给定的回调函数执行次数限制在一个给定的秒数内。</p>
<p>当回调函数执行次数超过限制时， <code v-pre>attempt</code> 方法返回 <code v-pre>false</code> ；否则， <code v-pre>attempt</code> 方法将返回回调的结果或 <code v-pre>true</code> 。 <code v-pre>attempt</code> 方法接受的第一个参数是一个速率限制器 「key」 ，它可以是你选择的任何字符串，代表被限制速率的动作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>RateLimiter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$executed</span> <span class="token operator">=</span> <span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">attempt</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$perMinute</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 发送消息...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token variable">$executed</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">  <span class="token keyword">return</span> <span class="token string single-quoted-string">'Too many messages sent!'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="手动配置尝试次数" tabindex="-1"><a class="header-anchor" href="#手动配置尝试次数"><span>手动配置尝试次数</span></a></h3>
<p>如果您想手动与限流器交互，可以使用多种方法。例如，您可以调用 <code v-pre>tooManyAttempts</code> 方法来确定给定的限流器是否超过了每分钟允许的最大尝试次数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>RateLimiter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">tooManyAttempts</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token variable">$perMinute</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'Too many attempts!'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，您可以使用 <code v-pre>remaining</code> 方法检索给定密钥的剩余尝试次数。如果给定的密钥还有重试次数，您可以调用 <code v-pre>hit</code> 方法来增加总尝试次数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>RateLimiter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">remaining</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token variable">$perMinute</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">hit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 发送消息...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确定限流器可用性" tabindex="-1"><a class="header-anchor" href="#确定限流器可用性"><span>确定限流器可用性</span></a></h4>
<p>当一个键没有更多的尝试次数时，<code v-pre>availableIn</code> 方法返回在尝试可用之前需等待的剩余秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>RateLimiter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">tooManyAttempts</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token variable">$perMinute</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$seconds</span> <span class="token operator">=</span> <span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">availableIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'You may try again in '</span><span class="token operator">.</span><span class="token variable">$seconds</span><span class="token operator">.</span><span class="token string single-quoted-string">' seconds.'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="清除尝试次数" tabindex="-1"><a class="header-anchor" href="#清除尝试次数"><span>清除尝试次数</span></a></h3>
<p>您可以使用 <code v-pre>clear</code> 方法重置给定速率限制键的尝试次数。例如，当接收者读取给定消息时，您可以重置尝试次数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Message</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>RateLimiter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 标记消息为已读。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">read</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Message</span> <span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Message</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'send-message:'</span><span class="token operator">.</span><span class="token variable">$message</span><span class="token operator">-></span><span class="token property">user_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$message</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/rate-limiting/14874" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/ra...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/rate-limiting/14874" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/ra...</a></p>
</blockquote>
</div></template>


